package com.dev.service;

/**
 * @author : fujc-dev@qq.com
 * @motto : talk is cheap, show me the code. salute the future!
 */

public class MyService {


    static int sumOfPrimes(int max) { // +1
        int total = 0;
        // +1
        OUT: for (int i = 1; i <= max; ++i) {
            // +1
            for (int j = 2; j < i; ++j) {
                // +1
                if (i % j == 0) {
                    continue OUT;
                }
            }
            total += i;
        }
        return total;
    }
    /**
     * 素数求和
     * <p>整个代码代码的复杂度就是7</p>
     * <p>
     * 优化：
     * </p>
     *
     * @param max
     * @return
     */
    static int sunOfPrimes(int max) {
        int total = 0;
        //复杂度 +1
        for (int i = 1; i <= max; i++) {
            //复杂度 +2
            for (int j = 2; j < i; ++j) {
                //复杂度 +3
                if (i % j == 0) {
                    System.out.println("1");
                    continue;
                }
            }
            System.out.println("--- " + i);
            total += i;
        }
        return total;
    }


    private static int sunOfPrimesExtend(int max) {
        int total = 0;
        //复杂度 +1
        for (int i = 1; i <= max; i++) {
            total += doSum(i);
        }
        return total;
    }

    private static int doSum(int i) {
        //复杂度 +1
        for (int j = 2; j < i; ++j) {
            //复杂度 +2
            if (i % j == 0) {
                continue;
            }
        }
        return i;
    }

    public static void main(String[] args) {
        System.out.println(sumOfPrimes(10));
        System.out.println(sunOfPrimesExtend(10));
    }

}
